<?php

namespace App\Models\Factory;

use App\Constants\ProductConstant;
use App\Helpers\DateUtils;
use App\Helpers\LinkUtils;
use App\Helpers\RestResponse;
use App\Helpers\RestResponseFactory;
use App\Helpers\RestUtils;
use App\Models\AbsModelFactory;
use App\Models\Orm\ApplyProcess;
use App\Models\Orm\CreditCardBanner;
use App\Models\Orm\FavouritePlatform;
use App\Models\Orm\Platform;
use App\Models\Orm\PlatformComment;
use App\Models\Orm\PlatformProduct;
use App\Models\Orm\PlatformProductTagConfig;
use App\Models\Orm\PlatformProductUserGroup;
use App\Models\Orm\ProductPropertylog;
use App\Models\Orm\ProductTag;
use App\Models\Orm\TagSeo;
use App\Models\Orm\Welfare;
use App\Services\Core\Store\Qiniu\QiniuService;
use App\Strategies\PageStrategy;
use Illuminate\Support\Facades\DB;

/**
 * 福利工厂
 * Class WelfareFactory
 * @package App\Models\Factory
 */
class WelfareFactory extends AbsModelFactory
{


    /**
     * 获取福利数据
     */
    public static function getwelfare()
    {
        $welfare = Welfare::select(['id','name', 'image','link','status'])
            ->orderby('id', 'desc')
            ->where(['status' => 1])
            ->get();
        return $welfare;
    }

    /**
     * 通过id获取福利链接
     */
    public static function getwelfareById($data)
    {
        $welfare = Welfare::where(['id'=> $data])->select(['link'])
            ->first();
        return $welfare;
    }

    /**
     * 获取彩票数据
     */
    public static function GetLottery($token)
    {
        $user  = UserFactory::getUserByToken($token);
        $data2['appKey']      = 'xianjindai';
        $data2['Uid']         = 'XJD'.$user['id'];
        $data2['nickName']    = '';
        $data2['mobilePhone'] =$user['userphone'];
        $data2['imageUrl']    = '';
        $data2['timeStamp']   = time();
        $data2['attach']      = '';
//        $data1 = "appKey=xianjindai&uid=XJD".$user['id']."&nickName=".$user['nickname']."&mobilePhone=".$user['userphone']."imageUrl=&timeStamp=".time()."&attach=";
//        $data1 = "appKey=xianjindai&attach=&imageUrl=&mobilePhone=13000000011&nickName=&timeStamp=1497598149&uid=0000000045";
        $data1 = "appKey=xianjindai&attach=&imageUrl=&mobilePhone=".$user['userphone']."&nickName="."&timeStamp=".time()."&uid=XJD".$user['id'];
        $data2['sign']        = self::sign($data1);
        $data2['signType']    = "RSA";
        $data2 = json_encode($data2,true);
        $data2 = urlencode($data2);
        $data3['link'] ="http://h5.jdd.com/common/jump/index.html?data=".$data2."&jump=&frm=xjd";

        $data3['isSuccess'] = "1";
        if ($user = "1") {
            return $data3;
        }else{
            return RestResponseFactory::ok(RestUtils::getStdObj(), RestUtils::getErrorMessage(1206), 1206);
        }
    }
    private static function sign($data) {

        $priKey = '-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDPOkXUBlKV+nifx09UxuG69wRHrf0etSiJ1fSm+tlFY4991MoT
gIvJbO8z9spSEtYItSz3EbZ1WnS3Ve9dncWovCdoREhnZuYBwNECHluS8mZhKYrm
RXZB6XxdB5L3bz8rbSdeYuRAH+59q2zPHrqfo0Dr1tziKbkUtNxv4Noq+wIDAQAB
AoGAYY6YcxGq7YpmISpOkaWzfTZvIGOrpzo35HQCYl3zzXPqiJ2VqIq6xE/xaLBF
XVAazSymB4aiNCsqiWj+cD8nXsfVy22wZGdxaPSwCLwOy6Mlf+Z7ihoFyeSGL8gV
2PicWHJni8frR/oMU6yHTcpPw2wj92FD1QOW9HLClmcqa9kCQQD61EXIdFZlmpxb
veqg0vGeturO7vBgu5FJVYpyUm4zGFhXco4uMRzJ1rvyzzcC/quwg3kAEjvgdJsT
Sc1natD/AkEA03/lpKDUQtZZeHt4PMa9I+qDvOFOMJoIM8Dth98XP+hi1MmUhusU
fU/oWMfbyQNBOQhP0nEmwGKnCXTBHEXqBQJAMOe2e3EDb2gH2HNdDeJTLejoO1Sn
FZU8rJm7FrMvvVKf1t0FD8iPnmL53ku8pB7XhuD7dKIZfpf9rHMVU3amLQJABGlb
ieaM5uP/s8KnjGT81v3yR4NPlTXxS9sPWVEu8/dN8HBP98x5fe+nwgpRLE00FBN3
TsVrcIHeSBU2XfebXQJBAKpQEPjAXSxHrszOFsxjbVHCSopHJaotcalIuSoSQZ/U
oyf9j3yIfrMDlEBb+9tR1yvwYXTKKTEX2ZVTin2smKs=
-----END RSA PRIVATE KEY-----';

        //转换为openssl密钥，必须是没有经过pkcs8转换的私钥

        $res = openssl_get_privatekey($priKey);

        //调用openssl内置签名方法，生成签名$sign

        openssl_sign($data, $sign, $res);


        openssl_free_key($res);

        $sign = base64_encode($sign);

        return $sign;

    }



}
